//
//  IssueViewController.swift
//  GitOSC-Swift
//
//  Created by 李萍 on 16/3/10.
//  Copyright © 2016年 李萍. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import ThPullRefresh

class IssueViewController: UITableViewController {
    var nameSpace: String!
    var issues: [GTIssue] = []
    
    var issueCell: IssueCell!
    
    var page: NSInteger = 1
    var isHeadRefresh: Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        
        self.tableView.registerNib(UINib(nibName: "IssueCell", bundle: nil), forCellReuseIdentifier: "issueCell")
        issueCell = self.tableView.dequeueReusableCellWithIdentifier("issueCell") as! IssueCell
        
        self.fetchForIssue()
        
        self.tableView.tableFooterView = UIView()
        self.tableView.addHeadRefresh(self) { () -> () in
            
            self.isHeadRefresh = true
            self.page = 1
            self.fetchForIssue()
        }
        self.tableView.addFootRefresh(self) { () -> () in
            
            self.isHeadRefresh = false
            ++self.page
            self.fetchForIssue()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //列表
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if issues.count > 0 {
            return issues.count
        } else {
            return 0
        }
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if issues.count > 0 {
            let issue = issues[indexPath.row]
            issueCell.issueTitle.text = issue.title
            let height = issueCell.issueTitle.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
            
            return height + 65
        } else {
            return 40
        }
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let issueCell = tableView.dequeueReusableCellWithIdentifier("issueCell", forIndexPath: indexPath) as? IssueCell
        
        if indexPath.row < issues.count {
            
            let issue = issues[indexPath.row]
            issueCell?.setContentForIssue(issue)
        }
        
        return issueCell!
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    
    //获取数据
    func fetchForIssue() {
        let userinfos = UserInfos()
        let private_token = userinfos.getprivateToken()
        
        let strUrl = "https://git.oschina.net/api/v3/projects/\(nameSpace)/issues?private_token=\(private_token)"+"&page="+"\(page)"
        
        if self.isHeadRefresh {
            self.issues.removeAll(keepCapacity: Bool())
        }
        
        Alamofire.Manager.sharedInstance.request(Method.GET, strUrl).responseJSON { response in
            if let results = response.result.value {
                let jsonArray = JSON(results).array!
                
                for i in 0 ..< jsonArray.count {

                    let issue = GTIssue(json: jsonArray[i])
                    self.issues.append(issue)
                }
                
                self.tableView.reloadData()
                self.tableView.tableHeadStopRefreshing()
                self.tableView.tableFootStopRefreshing()
            }
        }
    }
}
